<!-- 题目管理 -->

<template>
  <div>
    <div style="padding: 10px 0">
      <el-button icon="el-icon-circle-plus-outline" type="primary" @click="addHandle">{{
          $t('main.Insert')
        }}
      </el-button>
      <el-popconfirm
          style="margin: 0 10px 0 10px"
          icon="el-icon-warning"
          icon-color="red"
          :title="BatchDeletion_info"
          :confirm-button-text='Ok'
          :cancel-button-text='Cancel'
          @confirm="delBatch"
      >
        <el-button icon="el-icon-circle-close" type="danger" slot="reference" :disabled="flag">
          {{ $t('main.BatchDeletion') }}
        </el-button>
      </el-popconfirm>
      <div style="float: right">
        <el-input
            style="width: 200px;"
            :placeholder="Search_info"
            v-model="input"
            clearable @clear="clearEvent">
        </el-input>
        <el-select v-model="courseId" placeholder="请选择课程" style="width: 200px;margin-left: 5px">
          <el-option v-for="item in courses"
                     :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-select clearable @clear="clearEvent" v-model="type" placeholder="请选择题目类型"
                   style="width: 200px;margin-left: 5px">
          <el-option v-for="item in [{name: '选择题',value:1},{name: '判断题',value:2},{name: '问答题',value:3}]"
                     :key="item.value" :label="item.name" :value="item.value"></el-option>
        </el-select>
        <el-button icon="el-icon-refresh-right" style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
        <el-button icon="el-icon-search" style="margin-left: 5px" type="primary" @click="load">{{
            $t('main.Search')
          }}
        </el-button>
      </div>
    </div>

    <el-table border :data="tableData" stripe :header-cell-style="{background:'#DCDCDC',color:'#0f0f0f'}"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="id" label="ID" width="80" sortable>
      </el-table-column>
      <el-table-column prop="name" label="题目名称">
      </el-table-column>
      <el-table-column label="所属课程">
        <template v-slot="scope">
          <span v-for="item in courses" v-if="item.id == scope.row.courseId">{{ item.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" width="150">
        <template v-slot="scope">
          <span v-if="scope.row.type === 1">选择题</span>
          <span v-if="scope.row.type === 2">判断题</span>
          <span v-if="scope.row.type === 3">问答题</span>
        </template>
      </el-table-column>
      <el-table-column prop="a" label="A">
      </el-table-column>
      <el-table-column prop="b" label="B">
      </el-table-column>
      <el-table-column prop="c" label="C">
      </el-table-column>
      <el-table-column prop="d" label="D">
      </el-table-column>
      <el-table-column prop="score" label="分数">
      </el-table-column>
      <el-table-column prop="answer" label="答案">
      </el-table-column>
      <el-table-column prop="detail" label="解析">
      </el-table-column>
      <el-table-column prop="teacherName" label="出题人">
      </el-table-column>
      <el-table-column prop="time" label="出题时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" type="success" @click="editHandle(scope.row)">{{ $t('main.Edit') }}</el-button>
          <el-popconfirm
              icon="el-icon-warning"
              icon-color="red"
              :title="Delete_info"
              :confirm-button-text='Ok'
              :cancel-button-text='Cancel'
              @confirm="deleteHandle(scope.row.id)"
          >
            <el-button slot="reference" icon="el-icon-remove-outline" type="danger">{{ $t('main.Delete') }}</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0;text-align: right">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="题目信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="100px" size="small">
        <el-form-item label="题目类型">
          <el-select clearable v-model="form.type" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in [{name: '选择题',value:1},{name: '判断题',value:2},{name: '问答题',value:3}]"
                       :key="item.value" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属课程">
          <el-select clearable v-model="form.courseId" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in courses"
                       :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目名称">
          <el-input
              type="textarea"
              autosize
              v-model="form.name">
          </el-input>
        </el-form-item>
        <el-form-item label="A选项" v-if="form.type === 1">
          <el-input v-model="form.a" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="B选项" v-if="form.type === 1">
          <el-input v-model="form.b" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="C选项" v-if="form.type === 1">
          <el-input v-model="form.c" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="D选项" v-if="form.type === 1">
          <el-input v-model="form.d" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="分数">
          <el-input v-model="form.score" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="答案" v-if="form.type === 1">
          <el-radio v-model="form.answer" label="A">A</el-radio>
          <el-radio v-model="form.answer" label="B">B</el-radio>
          <el-radio v-model="form.answer" label="C">C</el-radio>
          <el-radio v-model="form.answer" label="D">D</el-radio>
        </el-form-item>
        <el-form-item label="答案" v-if="form.type === 2">
          <el-radio v-model="form.answer" label="√">√</el-radio>
          <el-radio v-model="form.answer" label="×">×</el-radio>
        </el-form-item>
        <el-form-item label="答案" v-if="form.type === 3">
          <el-input type="textarea" v-model="form.answer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="解析">
          <el-input
              type="textarea"
              autosize
              v-model="form.detail">
          </el-input>
        </el-form-item>
        <el-form-item label="出题时间">
          <el-date-picker v-model="form.time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择日期时间" style="width: 100%"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('main.Cancel') }}</el-button>
        <el-button type="primary" @click="save">{{ $t('main.Ok') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Management_Question",
  created() {
    this.LanguageMethod()
    this.load()
  },
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      input: '',
      form: {},
      courses: [],
      courseId: null,
      type: null,
      dialogFormVisible: false,
      multipleSelection: [],
      flag: true,
      // 当前语言
      currentLanguage: '',
      // 确定按钮
      Ok: '',
      // 取消按钮
      Cancel: '',
      // 表信息
      Table_info: '',
      // 删除信息
      Delete_info: '',
      // 批量删除信息
      BatchDeletion_info: '',
      // 搜索信息
      Search_info: '',
      // 标题，成功信息
      Title_Success_info: '',
      // 标题，失败信息
      Title_Error_info: '',
      // 删除成功消息
      Delete_Success_Message: '',
      // 删除失败消息
      Delete_Error_Message: '',
      // 操作成功信息
      Save_Success_Message: '',
      // 操作失败信息
      Save_Error_Message: '',
      // 批量删除成功信息
      DelBatch_Success_Message: '',
      // 批量删除失败信息
      DelBatch_Error_Message: '',
    }
  },
  methods: {
    clearEvent() {
      this.load()
    },
    // 请求后台数据
    load() {
      this.request.get("/question/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.input,
          a: this.input,
          b: this.input,
          c: this.input,
          d: this.input,
          answer: this.input,
          courseId: this.courseId,
          type: this.type
        }
      }).then(res => {
        this.tableData = res.records
        this.total = res.total
      })

      this.request.get("/course").then(res => {
        this.courses = res
      })
    },
    // 重置
    reset() {
      this.courseId = null
      this.type = null
      this.load()
    },
    // 总页数
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    // 当前页
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    // 新增
    addHandle() {
      this.form = {}
      this.dialogFormVisible = true
    },
    // 编辑
    editHandle(row) {
      this.form = Object.assign({}, row)
      this.form.courseId = this.form.courseId - 0
      this.dialogFormVisible = true
    },
    // 删除
    deleteHandle(id) {
      this.request.delete("/question/" + id).then(res => {
        if (res) {
          this.$message.success(this.Delete_Success_Message)
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error(this.Delete_Error_Message)
        }
      })
    },
    // 新增或编辑时点击确定，保存数据
    save() {
      this.request.post("/question", this.form).then(res => {
        if (res) {
          this.$message.success(this.Save_Success_Message)
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error(this.Save_Error_Message)
        }
      })
    },
    // 批量删除按钮状态
    handleSelectionChange(val) {
      this.multipleSelection = val
      // 当选择数据时，开启批量删除按钮
      if (this.multipleSelection.length === 0) {
        this.flag = true
      }
      // 没有选择数据，禁用批量删除按钮
      else {
        this.flag = false
        this.multipleSelection = val
      }
    },
    // 批量删除
    delBatch() {
      // 将对象数组转int数组
      let ids = this.multipleSelection.map(v => v.id)
      this.request.post("/question/del/batch", ids).then(res => {
        if (res) {
          this.$message.success(this.DelBatch_Success_Message)
          this.load()
        } else {
          this.$message.error(this.DelBatch_Error_Message)
        }
      })
    },
    // 语言设置
    LanguageMethod() {
      // 获取当前语言
      this.currentLanguage = localStorage.getItem('lang');

      // 中文
      if (this.currentLanguage === 'Chinese') {
        this.Ok = this.$i18n.messages.Chinese.main.Ok
        this.Cancel = this.$i18n.messages.Chinese.main.Cancel
        this.Table_info = this.$i18n.messages.Chinese.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.Chinese.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.Chinese.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.Chinese.main.Search_info
        this.Title_Success_info = this.$i18n.messages.Chinese.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.Chinese.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.Chinese.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.Chinese.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.Chinese.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.Chinese.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.Chinese.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.Chinese.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.Chinese.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.Chinese.main.ImportFile_Error
      }
      // 英语
      else if (this.currentLanguage === 'English') {
        this.Ok = this.$i18n.messages.English.main.Ok
        this.Cancel = this.$i18n.messages.English.main.Cancel
        this.Table_info = this.$i18n.messages.English.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.English.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.English.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.English.main.Search_info
        this.Title_Success_info = this.$i18n.messages.English.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.English.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.English.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.English.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.English.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.English.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.English.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.English.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.English.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.English.main.ImportFile_Error
      }
      // 日语
      else if (this.currentLanguage === 'Japanese') {
        this.Ok = this.$i18n.messages.Japanese.main.Ok
        this.Cancel = this.$i18n.messages.Japanese.main.Cancel
        this.Table_info = this.$i18n.messages.Japanese.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.Japanese.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.Japanese.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.Japanese.main.Search_info
        this.Title_Success_info = this.$i18n.messages.Japanese.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.Japanese.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.Japanese.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.Japanese.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.Japanese.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.Japanese.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.Japanese.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.Japanese.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.Japanese.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.Japanese.main.ImportFile_Error
      }
      // 韩语
      else if (this.currentLanguage === 'Korean') {
        this.Ok = this.$i18n.messages.Korean.main.Ok
        this.Cancel = this.$i18n.messages.Korean.main.Cancel
        this.Table_info = this.$i18n.messages.Korean.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.Korean.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.Korean.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.Korean.main.Search_info
        this.Title_Success_info = this.$i18n.messages.Korean.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.Korean.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.Korean.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.Korean.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.Korean.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.Korean.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.Korean.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.Korean.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.Korean.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.Korean.main.ImportFile_Error
      }
      // 俄语
      else if (this.currentLanguage === 'Russian') {
        this.Ok = this.$i18n.messages.Russian.main.Ok
        this.Cancel = this.$i18n.messages.Russian.main.Cancel
        this.Table_info = this.$i18n.messages.Russian.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.Russian.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.Russian.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.Russian.main.Search_info
        this.Title_Success_info = this.$i18n.messages.Russian.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.Russian.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.Russian.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.Russian.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.Russian.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.Russian.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.Russian.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.Russian.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.Russian.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.Russian.main.ImportFile_Error
      }
      // 德语
      else if (this.currentLanguage === 'German') {
        this.Ok = this.$i18n.messages.German.main.Ok
        this.Cancel = this.$i18n.messages.German.main.Cancel
        this.Table_info = this.$i18n.messages.German.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.German.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.German.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.German.main.Search_info
        this.Title_Success_info = this.$i18n.messages.German.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.German.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.German.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.German.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.German.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.German.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.German.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.German.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.German.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.German.main.ImportFile_Error
      }
      // 法语
      else if (this.currentLanguage === 'French') {
        this.Ok = this.$i18n.messages.French.main.Ok
        this.Cancel = this.$i18n.messages.French.main.Cancel
        this.Table_info = this.$i18n.messages.French.main.Table_info
        this.Delete_info = this.BatchDeletion_info = this.$i18n.messages.French.main.Delete_info
        this.BatchDeletion_info = this.$i18n.messages.French.main.BatchDeletion_info
        this.Search_info = this.$i18n.messages.French.main.Search_info
        this.Title_Success_info = this.$i18n.messages.French.main.Title_Success_info
        this.Title_Error_info = this.$i18n.messages.French.main.Title_Error_info
        this.Delete_Success_Message = this.$i18n.messages.French.main.Delete_Success
        this.Delete_Error_Message = this.$i18n.messages.French.main.Delete_Error
        this.Save_Success_Message = this.$i18n.messages.French.main.Save_Success
        this.Save_Error_Message = this.$i18n.messages.French.main.Save_Error
        this.DelBatch_Success_Message = this.$i18n.messages.French.main.DelBatch_Success
        this.DelBatch_Error_Message = this.$i18n.messages.French.main.DelBatch_Error
        this.ImportFile_Success_Message = this.$i18n.messages.French.main.ImportFile_Success
        this.ImportFile_Error_Message = this.$i18n.messages.French.main.ImportFile_Error
      }
    }
  }
}
</script>